<template>
  <a-spin :spinning="spinning != 0">
    <a-form :form="form">
      <a-form-item :label="$t('m.system.org.entity.parentId')" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="model.parentId">
        <sys-org-name v-model="model.parentId" placeholder="" :disabled="true"></sys-org-name>
      </a-form-item>
      <a-form-item :label="$t('m.system.org.entity.orgName')" :labelCol="labelCol" :wrapperCol="wrapperCol">
        <a-input v-decorator="[ 'orgName', validatorRules.orgName]" placeholder=""></a-input>
      </a-form-item>
      <a-form-item :label="$t('m.system.org.entity.orgCode')" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="model.orgCode">
        <a-input v-model="model.orgCode" placeholder="" :disabled="true"></a-input>
      </a-form-item>
      <a-form-item :label="$t('m.system.org.entity.orgType')" :labelCol="labelCol" :wrapperCol="wrapperCol">
        <a-radio-group v-decorator="[ 'orgType', validatorRules.orgType]" :options="options">
        </a-radio-group>
      </a-form-item>
      <a-form-item :label="$t('m.system.org.entity.orgNameAbbr')" :labelCol="labelCol" :wrapperCol="wrapperCol">
        <a-input v-decorator="[ 'orgNameAbbr', validatorRules.orgNameAbbr]" placeholder=""></a-input>
      </a-form-item>
      <a-form-item :label="$t('m.system.org.entity.orgOrder')" :labelCol="labelCol" :wrapperCol="wrapperCol">
        <a-input-number :min="0" :step="1" v-decorator="[ 'orgOrder', validatorRules.orgOrder]"
                        placeholder=""></a-input-number>
      </a-form-item>
    </a-form>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { FormMixin } from '@/mixins/FormMixin'
  import SysOrgName from '../../../components/ych/ych-org/SysOrgName'
  import { SystemConstant } from '../SystemConstant'
  import YchConstant from "@/api/YchConstant";

  export default {
    name: 'SysOrgModal',
    mixins: [FormMixin],
    components: {
      SysOrgName
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 4 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 20 }
        },
        validatorRules: {
          orgName: { rules: [{ required: true, message: this.$t('m.system.org.verify.orgName') }] },
          // orgCode: { rules: [{ required: true, message: '请输入组织编码!' }, { validator: this.checkCode }] }, // 暂时无用
          orgType: { rules: [{ required: true, message: this.$t('m.system.org.verify.orgType') }] },
          orgNameAbbr: { rules: [] },
          orgOrder: { rules: [{ required: true, message: this.$t('m.system.org.verify.orgOrder') }], initialValue: 0 }
        },
        options: SystemConstant.orgType,
        url: {
          queryById: YchConstant.PREFIX_SYSTEM + '/sysOrg/queryById',
          add: YchConstant.PREFIX_SYSTEM + '/sysOrg/add',
          edit: YchConstant.PREFIX_SYSTEM + '/sysOrg/edit'
          // checkCode: YchConstant.PREFIX_SYSTEM + '/sysOrg/checkCode'
        }
      }
    },
    computed: {
      parentId() {
        return this.model.parentId
      }
    },
    methods: {
      // 暂时无用
      /*checkCode(rule, value, callback) {
        if (!value) {
          callback()
          return
        }
        getAction(this.url.checkCode, {
          value: value,
          id: this.model.id
        }).then((res) => {
          res.success ? callback() : callback(res.message)
        })
      },*/
      createOptions() {
        if (!this.parentId) {
          this.options = SystemConstant.orgType
          return
        }
        getAction(this.url.queryById, { id: this.parentId }).then((res) => {
          if (res.success && res.result) {
            this.options = SystemConstant.orgType.filter(vo => vo.value >= res.result.orgType)
          } else {
            this.options = SystemConstant.orgType
          }
        }).finally(() => {
        })
      }
    },
    watch: {
      parentId: {
        handler() {
          this.createOptions()
        },
        immediate: true
      }
    }
  }
</script>
